//
//  SetData.m
//  OST
//
//  Created by mac on 13-7-11.
//  Copyright (c) 2013年 mac. All rights reserved.
//

#import "GpsPositionData.h"
#import "Common.h"

@interface GpsPositionData ()

@end

@implementation GpsPositionData
@synthesize ids;
@synthesize type;

- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSArray *datas= [reciveData componentsSeparatedByString:@"\n" ];
    NSMutableArray *result = [NSMutableArray array];
    
    int index = -1;
    if ([datas count]>1) {
        index = 1;
    }
    
    for (NSString *mainline in datas) {
        NSArray *subLines= [mainline componentsSeparatedByString:@"," ];
        if ([subLines count]>17) {
            NSMutableDictionary *dict = [NSMutableDictionary dictionary];
            [dict setValue:[NSString stringWithFormat:@"%@%@",[subLines objectAtIndex:0],(index!=-1)?[NSString stringWithFormat:@"[%d]",index]:@""] forKey:@"name"];
            [dict setValue:[subLines objectAtIndex:6] forKey:@"longitude"];
            [dict setValue:[subLines objectAtIndex:7] forKey:@"longitudeDirection"];
            [dict setValue:[subLines objectAtIndex:8] forKey:@"latitude"];
            [dict setValue:[subLines objectAtIndex:9] forKey:@"latitudeDirection"];
            [dict setValue:[subLines objectAtIndex:10] forKey:@"speed"];
            
            NSString *item2 = [subLines objectAtIndex:2];
            NSString *item1 = [subLines objectAtIndex:2];
            NSString *time =[NSString stringWithFormat:@"20%@-%@-%@ %@:%@:%@",[item2 substringWithRange:NSMakeRange(4, 2)],[item2 substringWithRange:NSMakeRange(2, 2)],[item2 substringWithRange:NSMakeRange(0, 2)],[item1 substringWithRange:NSMakeRange(0, 2)],[item1 substringWithRange:NSMakeRange(2, 2)],[item1 substringWithRange:NSMakeRange(4, 2)]];
            
            [dict setValue:time forKey:@"time"];
            
            if ([subLines count]>=17) {
                [dict setValue:[subLines objectAtIndex:17] forKey:@"errorSpan"];
            }
            [result addObject:dict];
        }
    }
    
    [self doneLoadData:result];
}

-(void)doneLoadData:(id)data
{
    [super doneLoadData:data];
}

-(void)reLoadData
{
    if (type == 1) {
        self.httpRequest = [Common createHTTPRequest:[NSString stringWithFormat:@"cgi-bin/getDB.cgi?R=0&TN=cur_%@",ids] delegate:self];
    }else{
        self.httpRequest = [Common createHTTPRequest:[NSString stringWithFormat:@"cgi-bin/getDB.cgi?R=1&TN=%@",ids] delegate:self];
    }
    [self.httpRequest startAsynchronous];
}



@end
